var objBoard = {
    id: "sdcbaseboard",
    selectedQCEntity:{},
    stage:[],
    selectedStage:{},
    selectedEvent:{}
};

objBoard.QCEntity = $("#QCEntity").combobox({
    valueField: 'id',
    textField: 'btDesc',
    onShowPanel: function () { // 只有在下拉层显示时,才去关联url拉取数据,提高首屏速度
        var url = $(this).combobox('options').url;
        if (!url) {
            var url = Sys.backendPath + "/sds/sdsqcentity/noPageList";
            $(this).combobox('reload', url);
        }
    },
    onSelect: function (record) {
        objBoard.selectedQCEntity=record;
        objBoard.clearStage();
        objBoard.getStage();
    }
});

objBoard.clearStage = function () {
    var stage = $('#QCStage').accordion('panels');
    for (var index = stage.length - 1; index > -1; index--) {
        $('#QCStage').accordion('remove', index);
    }
}

objBoard.getStage = function(){
    var ajax = new $ax(Sys.backendPath + "/sdc/sdcbasestage/query", function (data) {
        for (var index = 0; index < data.length; index++) {
            var stage = data[index];
            $('#QCStage').accordion('add', {
                id:"stage_"+stage.id,
                title: stage.description,
                content: '请稍等...',
                selected: false,
                href:Sys.backendPath + "/sdc/sdcbaseevent/ul",
                method:"post",
                queryParams:{"stageId":stage.id}
            });
        }
        objBoard.stage=data;
    }, function (data) {
        Sys.error("获取阶段失败!" + data.responseJSON.message + "!");
    });
    ajax.set("qcentityId",objBoard.selectedQCEntity.id);
    ajax.set("isActive",1);
    ajax.start();
}

//阶段选择
$('#QCStage').accordion({    
    onSelect:function(title,index){
        var theStage=objBoard.stage[index];
        if (!theStage) return;
        objBoard.selectedStage=theStage;
    }
});

//事件选择
$('#QCStage').on('click','.event_item',function (e) {
    e.preventDefault();
    $('.event_item').removeClass('active');
    $(this).addClass("active");
    
    var event_id=$(this)[0].id.split("_")[1];
    var ajax = new $ax(Sys.backendPath + "/sdc/sdcbaseevent/evt/"+event_id, function (data) {
        objBoard.selectedEvent=data;
        $("#code").val(objBoard.selectedEvent.code);
        $("#description").val(objBoard.selectedEvent.description);
        $("#expression").val(objBoard.selectedEvent.expression);
        $("#comment").val(objBoard.selectedEvent.comment);
        
    }, function (data) {
        Sys.error("获取阶段失败!" + data.responseJSON.message + "!");
    });
    ajax.start();
    
    SdcBasePlan.table.clear();
    SdcBasePlan.table.refresh({query: {eventId:event_id}});
});

$("#saveEvent").on("click",function() {
    var ajax = new $ax(Sys.backendPath + "/sdc/sdcbaseevent/update_info", function (data) {
        Sys.success("保存成功");
    }, function (data) {
        Sys.error("保存失败!" + data.responseJSON.message + "!");
    });
    ajax.set("eventId",objBoard.selectedEvent.id);
    ajax.set("description",$("#description").val());
    ajax.set("expression",$("#expression").val());
    ajax.set("comment",$("#comment").val());
    ajax.start();
})